home *** CD-ROM | disk | FTP | other *** search
- Path: news.microsoft.com!news
- From: a-cnadc@microsoft.com (Dann Corbit)
- Newsgroups: comp.lang.c
- Subject: Re: Array Parameters
- Date: 4 Jan 1996 21:55:12 GMT
- Organization: Microsoft Corporation
- Message-ID: <4chic0$mkd@news.microsoft.com>
- References: <wayne.820650643@hawk> <4ce349$4j9@hacgate2.hac.com> <820701694snz@genesis.demon.co.uk> <4cg104$qmp@nervous.pdb.sni.de> <4cgmuc$k9k@hacgate2.hac.com>
- NNTP-Posting-Host: 157.57.171.202
- Mime-Version: 1.0
- X-Newsreader: WinVN 0.93.14
-
- In article <4cgmuc$k9k@hacgate2.hac.com>, collins@thor.tu.hac.com says...
- >
- >Josef Moellers (mollers.pad@sni.de) wrote:
- >: In <820701694snz@genesis.demon.co.uk> Lawrence Kirby <fred@genesis.demon.co.uk> writes:
- >
- >: [ ... ]
- >
- >: > It is impossible
- >: >to specify an array as a function parameter since, as in the example above,
- >
- >: Well, if you MUST pass an array by value and not by reference, wrap it
- >: into a structure:
- >
- >: struct foo {
- >: int array[100];
- >: };
- >: ...
- >
- >
- >I don't get it ... what does this buy me ("me" == any competant C programmer)?
- >I'm still going to get a pointer-to-something, whether it's a pointer-to-
- >structure, pointer-to-array, or pointer-to-first-element. I still need to
- >know which one is used so I can access the array elements properly. (Although,
- >there is seldom any difference between passing a pointer to an array, and
- >a pointer to the first element of that array).
- >
- >BTW, passing a pointer to an object is _not_ the same as pass by reference.
- >C does not support pass-by-reference at all.
- If you pass the struct by value you will get a copy of the whole structure.
- This means that:
- 1. All sizeof( struct foo ) bytes are piled onto the stack
- 2. You can change the contents of the array in the function without
- changing the contents of the array in the calling program.
-
- #include <stdio.h>
- #include <string.h>
- typedef struct foo {
- char pszData[100];
- } footype;
- int iDontClobberTheFoo( footype );
- int main()
- {
- footype myFoo;
- strcpy(myFoo.pszData, "Hi there, my name is skinner the grinner");
- puts("Foo before call");
- puts( myFoo.pszData );
- iDontClobberTheFoo( myFoo );
- puts("Foo after call");
- puts( myFoo.pszData );
- return 0;
- }
- int iDontClobberTheFoo( footype aFooCopy )
- {
- strcpy(aFooCopy.pszData, "This data is completely different");
- puts("Foo during call");
- puts( aFooCopy.pszData );
- return 0;
- }
- /* The program above outputs the following on my compiler:
- Foo before call
- Hi there, my name is skinner the grinner
- Foo during call
- This data is completely different
- Foo after call
- Hi there, my name is skinner the grinner
- */
-
- --
- The opinions expressed in this message are my own personal views
- and do not reflect the official views of Microsoft Corporation.
-
-